/*
 * TARDISDataDepositorView.java
 */

package tardisdatadepositor;

import java.awt.Event;
import java.awt.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListSelectionEvent;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.regex.Pattern;
import javax.swing.DefaultListModel;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionListener;
import monash.med.crystallography.DataSetTools.DiffdumpXML.DiffdumpOut;


/**
 * TODO:
 *
 * Bug where index gets shifted, so hash map isn't accurate anymore..
 * Button to remove from lists
 * GO Button
 * Alignments
 * Copy operation (design also?)
 * Trawl through objects and write METS
 * Validation
 */
public class TARDISDataDepositorView extends FrameView {

    HashMap stateMap = new HashMap();

    int lastSelected = 0;

    DefaultListModel listModel;
    DefaultListModel pdbsListModel;
    DefaultListModel citationsListModel;

    boolean save = true;

    ListSelectionModel listSelectionModel;

        //Create a file chooser
        final JFileChooser fc = new JFileChooser();

        class MyListDataListener implements ListDataListener {

            public void intervalAdded(ListDataEvent e) {

                int i = e.getIndex0();

//                if(listModel.get(i).equals("Raw Dataset"))
//                {
//                    stateMap.put(i, new DatasetInfo(new File(""), "", "", "", 0f, "", "", ""));
//                }
//                else if(listModel.get(i).equals("Ancillary Dataset"))
//                {
//                    stateMap.put(i, new DatasetInfo("", new File(".")));
//                }
//                else if(listModel.get(i).equals("Experiment"))
//                {
//                    stateMap.put(i, new ExperimentInfo(new ArrayList<String>(), "", new ArrayList<String>(), "", new ArrayList<String>(), new File(""), ""));
//
//                }
            }

            public void intervalRemoved(ListDataEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
                save = false;
            }

            public void contentsChanged(ListDataEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

        }

        class SharedListSelectionHandler implements ListSelectionListener {
            public void valueChanged(ListSelectionEvent e) {
                ListSelectionModel lsm = (ListSelectionModel)e.getSource(); // this is the key to knowing the source, it's the hash map key
                //jList1.getSelectedValue() is this the key to getting the object?
                //grab the object with the getselectedvalue, then update it in the jlist by 'adding' at the selected index

                int firstIndex = e.getFirstIndex();
                int lastIndex = e.getLastIndex();
                boolean isAdjusting = e.getValueIsAdjusting();
//                jTextArea1.append("Event for indexes "
//                              + firstIndex + " - " + lastIndex
//                              + "; isAdjusting is " + isAdjusting
//                              + "; selected indexes:");

                if(isAdjusting)
                {
                    if (lsm.isSelectionEmpty()) {
                        abstractExperiment.append(" <none>");
                    } else {
                        // Find out which indicies are selected.
                        int minIndex = lsm.getMinSelectionIndex();
                        int maxIndex = lsm.getMaxSelectionIndex();

                        for (int i = minIndex; i <= maxIndex; i++) {
                            if (lsm.isSelectedIndex(i)) {
    //                            jTextArea1.append(" " + i);

                                int from = 0;
                                int to = i;

                                  if(firstIndex == lastIndex)
                                  {
                                      //System.out.println("New Selection");
                                  }
                                  else if(i == firstIndex)
                                  {
                                     //System.out.println("From: " + lastIndex + " to " + i);
                                     from = lastIndex;
                                  }
                                  else
                                  {
                                     //System.out.println("From: " + firstIndex + " to " + i);
                                     from = firstIndex;         
                                  }

                                  // Save function, takes object and index
                                  // Load function, takes index

                                    // SAVE
                                    //check instance
                                    Object info = listModel.get(from);

                                    saveInterfaceState(info, from);

                                    // LOAD
                                    Object infoLoad = listModel.get(to);

                                    if(infoLoad instanceof ExperimentInfo)
                                    {
                                        enableExperimentView();

                                        ExperimentInfo experimentInfo = (ExperimentInfo) infoLoad;
                                        titleExperiment.setText(experimentInfo.getTitle()); // Load each field of opening view

                                        String authorString = "";

                                        for(String author : experimentInfo.getAuthors())
                                        {
                                            if(authorString.equals(""))
                                            {
                                                authorString = author;
                                            }
                                            else
                                            {
                                                authorString = authorString + ", " + author;
                                            }
                                        }

                                        authors.setText(authorString);
                                        abstractExperiment.setText(experimentInfo.getabstractTxt());
                                        pathForDataExperiment.setText(experimentInfo.getDestDir().getAbsolutePath());
                                        institutionName.setText(experimentInfo.getInstitutionName());
                                        //PDBs
                                        //citationURLs

                                    }
                                    else if(infoLoad instanceof DatasetInfo)
                                    {

                                        DatasetInfo datasetInfo = (DatasetInfo) infoLoad;

                                        if(datasetInfo.isRawImages())
                                        {
                                            enableRawDatasetView();
                                            descriptionRaw.setText(datasetInfo.getDescription()); // Load each field of opening view
                                            chiAngle.setText(datasetInfo.getChiAngle());
                                            diffractometerType.setText(datasetInfo.getDiffractometerType());
                                            crystalName.setText(datasetInfo.getCrystalName());
                                            xraySource.setText(datasetInfo.getXraySource());

                                            if(datasetInfo.getResolutionLimit() != 0) // make sure nothing invalid is stored
                                            {
                                                resolutionLimit.setText(String.valueOf(datasetInfo.getResolutionLimit()));
                                            }
                                            else
                                            {
                                                resolutionLimit.setText("0.0");
                                            }

                                            pathToDataRaw.setText(datasetInfo.getDir());
                                            mosaicSpread.setText(datasetInfo.getMosaicSpread());

                                        }
                                        else
                                        {
                                            enableAncillaryDatasetView();
                                            descriptionAncillary.setText(datasetInfo.getDescription()); // Load each field of opening view
                                            pathToDataAncillary.setText(datasetInfo.getDir());

                                        }
                                    }
                               
                            }
                        }
                    }
                }
            }

        }

    void enableExperimentView()
    {
        jLayeredPane1.setVisible(true);
        jLayeredPane2.setVisible(false);
        jLayeredPane3.setVisible(false);
        jLayeredPane4.setVisible(false);
    }

    void enableRawDatasetView()
    {
        jLayeredPane1.setVisible(false);
        jLayeredPane2.setVisible(true);
        jLayeredPane3.setVisible(false);
        jLayeredPane4.setVisible(false);
    }

    void enableAncillaryDatasetView()
    {
        jLayeredPane1.setVisible(false);
        jLayeredPane2.setVisible(false);
        jLayeredPane3.setVisible(true);
        jLayeredPane4.setVisible(false);
    }

    void enableConsoleView()
    {
        jLayeredPane1.setVisible(false);
        jLayeredPane2.setVisible(false);
        jLayeredPane3.setVisible(false);
        jLayeredPane4.setVisible(true);
    }

    private void saveInterfaceState(Object info, int from)
    {
        if(info instanceof ExperimentInfo)
        {
            ExperimentInfo experimentInfo = (ExperimentInfo) info;
            experimentInfo.setTitle(titleExperiment.getText()); // Save each field of closing view

            ArrayList authorList = new ArrayList();

            for(String author : authors.getText().split(","))
            {
                authorList.add(author.trim());
            }

            experimentInfo.setAuthors(authorList); // iterate properly

            experimentInfo.setabstractTxt(abstractExperiment.getText());
            experimentInfo.setDestDir(new File(pathForDataExperiment.getText()));
            experimentInfo.setInstitutionName(institutionName.getText());

            ArrayList<String> citationURLs = new ArrayList<String>();

            for(Object citationURLobj : citationsListModel.toArray())
            {
                String citationURL = (String) citationURLobj;

                citationURLs.add(citationURL);
            }

            experimentInfo.setRelations(citationURLs);

            ArrayList<String> pdbURLs = new ArrayList<String>();

            for(Object pdbURLobj : pdbsListModel.toArray())
            {
                String pdbURL = (String) pdbURLobj;

                pdbURLs.add(pdbURL);
            }

            experimentInfo.setPdbid(pdbURLs);


            listModel.set(from, experimentInfo);
        }
        else if(info instanceof DatasetInfo)
        {
            DatasetInfo datasetInfo = (DatasetInfo) info;

            if(datasetInfo.isRawImages())
            {
                datasetInfo.setDescription(descriptionRaw.getText()); // Save each field of closing view
                datasetInfo.setChiAngle(chiAngle.getText());
                datasetInfo.setDiffractometerType(diffractometerType.getText());
                datasetInfo.setCrystalName(crystalName.getText());
                datasetInfo.setXraySource(xraySource.getText());

                try {
                    if(!resolutionLimit.getText().equals(""))
                    {
                       datasetInfo.setResolutionLimit(new Float(resolutionLimit.getText()));
                    }
                    else
                    {
                        datasetInfo.setResolutionLimit(new Float(0.0f));
                    }
                }
                catch(NumberFormatException e)
                {
                    datasetInfo.setResolutionLimit(new Float(0.0f));
                }

                // because it has a default
                
                datasetInfo.setDir(pathToDataRaw.getText());

                datasetInfo.setMosaicSpread(mosaicSpread.getText());

                listModel.set(from, datasetInfo);

            }
            else
            {
                datasetInfo.setDescription(descriptionAncillary.getText()); // Save each field of closing view
                datasetInfo.setDir(pathToDataAncillary.getText());
                //stateMap.put(from, datasetInfo);
                listModel.set(from, datasetInfo);
            }
        }
    }

    public TARDISDataDepositorView(SingleFrameApplication app) {
        super(app);

        initComponents();

        listModel = new DefaultListModel(); // steve
        jList1.setModel(listModel);

        pdbsListModel = new DefaultListModel();
        PDBs.setModel(pdbsListModel);

        citationsListModel = new DefaultListModel();
        citationURLs.setModel(citationsListModel);

        listSelectionModel = jList1.getSelectionModel();
        listSelectionModel.addListSelectionListener(
                            new SharedListSelectionHandler());
        listModel.addListDataListener(new MyListDataListener());

        listModel.addElement(new ExperimentInfo(new ArrayList<String>(), "", new ArrayList<String>(), "", new ArrayList<String>(), new File(""), ""));
        jList1.setSelectedIndex(0);
        enableExperimentView();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);

        System.setOut( new PrintStream(new ConsoleView (console, System.out), true));

    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = TARDISDataDepositorApp.getApplication().getMainFrame();
            aboutBox = new TARDISDataDepositorAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        TARDISDataDepositorApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jLayeredPane3 = new javax.swing.JLayeredPane();
        pathToDataAncillary = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        browseAncillary = new javax.swing.JButton();
        descriptionAncillary = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        chiAngle = new javax.swing.JTextField();
        diffractometerType = new javax.swing.JTextField();
        crystalName = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        xraySource = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        resolutionLimit = new javax.swing.JTextField();
        descriptionRaw = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        pathToDataRaw = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        browseRaw = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        mosaicSpread = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        removePDBs = new javax.swing.JButton();
        abstractExperimentScroll = new javax.swing.JScrollPane();
        abstractExperiment = new javax.swing.JTextArea();
        removeCitationURLs = new javax.swing.JButton();
        authors = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        pathForDataExperiment = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        PDBsScroll = new javax.swing.JScrollPane();
        PDBs = new javax.swing.JList();
        addPDBs = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        citationURLsScroll = new javax.swing.JScrollPane();
        citationURLs = new javax.swing.JList();
        addCitationURLs = new javax.swing.JButton();
        browseExperiment = new javax.swing.JButton();
        institutionName = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        titleExperiment = new javax.swing.JTextField();
        jLayeredPane4 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        console = new javax.swing.JTextArea();
        jLabel21 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        packageButton = new javax.swing.JButton();
        showConsole = new javax.swing.JButton();
        jLabel22 = new javax.swing.JLabel(createImageIcon("/images/data_packager.jpg",
            "a pretty but meaningless splat"));
    menuBar = new javax.swing.JMenuBar();
    javax.swing.JMenu fileMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
    javax.swing.JMenu helpMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
    statusPanel = new javax.swing.JPanel();
    javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
    statusMessageLabel = new javax.swing.JLabel();
    statusAnimationLabel = new javax.swing.JLabel();
    jInternalFrame1 = new javax.swing.JInternalFrame();
    jSeparator1 = new javax.swing.JSeparator();
    popupMenu1 = new java.awt.PopupMenu();

    mainPanel.setName("mainPanel"); // NOI18N
    mainPanel.setPreferredSize(new java.awt.Dimension(935, 790));

    jLayeredPane3.setName("jLayeredPane3"); // NOI18N

    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(tardisdatadepositor.TARDISDataDepositorApp.class).getContext().getResourceMap(TARDISDataDepositorView.class);
    pathToDataAncillary.setText(resourceMap.getString("pathToDataAncillary.text")); // NOI18N
    pathToDataAncillary.setName("pathToDataAncillary"); // NOI18N
    pathToDataAncillary.setBounds(45, 90, 370, 20);
    jLayeredPane3.add(pathToDataAncillary, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel18.setText(resourceMap.getString("jLabel18.text")); // NOI18N
    jLabel18.setName("jLabel18"); // NOI18N
    jLabel18.setBounds(45, 60, 160, 16);
    jLayeredPane3.add(jLabel18, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
    jLabel2.setName("jLabel2"); // NOI18N
    jLabel2.setBounds(45, 8, 160, 16);
    jLayeredPane3.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

    browseAncillary.setText(resourceMap.getString("browseAncillary.text")); // NOI18N
    browseAncillary.setName("browseAncillary"); // NOI18N
    browseAncillary.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            browseAncillaryActionPerformed(evt);
        }
    });
    browseAncillary.setBounds(437, 85, 100, 29);
    jLayeredPane3.add(browseAncillary, javax.swing.JLayeredPane.DEFAULT_LAYER);

    descriptionAncillary.setText(resourceMap.getString("descriptionAncillary.text")); // NOI18N
    descriptionAncillary.setName("descriptionAncillary"); // NOI18N
    descriptionAncillary.setBounds(45, 140, 370, 28);
    jLayeredPane3.add(descriptionAncillary, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel19.setText(resourceMap.getString("jLabel19.text")); // NOI18N
    jLabel19.setName("jLabel19"); // NOI18N
    jLabel19.setBounds(45, 120, 140, 16);
    jLayeredPane3.add(jLabel19, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLayeredPane2.setName("jLayeredPane2"); // NOI18N

    chiAngle.setText(resourceMap.getString("chiAngle.text")); // NOI18N
    chiAngle.setName("chiAngle"); // NOI18N
    chiAngle.setBounds(140, 470, 180, 28);
    jLayeredPane2.add(chiAngle, javax.swing.JLayeredPane.DEFAULT_LAYER);

    diffractometerType.setText(resourceMap.getString("diffractometerType.text")); // NOI18N
    diffractometerType.setName("diffractometerType"); // NOI18N
    diffractometerType.setBounds(140, 250, 180, 28);
    jLayeredPane2.add(diffractometerType, javax.swing.JLayeredPane.DEFAULT_LAYER);

    crystalName.setText(resourceMap.getString("crystalName.text")); // NOI18N
    crystalName.setName("crystalName"); // NOI18N
    crystalName.setBounds(140, 350, 180, 30);
    jLayeredPane2.add(crystalName, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel15.setText(resourceMap.getString("jLabel15.text")); // NOI18N
    jLabel15.setName("jLabel15"); // NOI18N
    jLabel15.setBounds(140, 330, 180, 16);
    jLayeredPane2.add(jLabel15, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel14.setText(resourceMap.getString("jLabel14.text")); // NOI18N
    jLabel14.setName("jLabel14"); // NOI18N
    jLabel14.setBounds(140, 280, 170, 16);
    jLayeredPane2.add(jLabel14, javax.swing.JLayeredPane.DEFAULT_LAYER);

    xraySource.setText(resourceMap.getString("xraySource.text")); // NOI18N
    xraySource.setName("xraySource"); // NOI18N
    xraySource.setPreferredSize(new java.awt.Dimension(84, 28));
    xraySource.setBounds(140, 300, 180, 28);
    jLayeredPane2.add(xraySource, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
    jLabel12.setName("jLabel12"); // NOI18N
    jLabel12.setBounds(140, 180, 170, 16);
    jLayeredPane2.add(jLabel12, javax.swing.JLayeredPane.DEFAULT_LAYER);

    resolutionLimit.setText(resourceMap.getString("resolutionLimit.text")); // NOI18N
    resolutionLimit.setName("resolutionLimit"); // NOI18N
    resolutionLimit.setBounds(140, 200, 80, 28);
    jLayeredPane2.add(resolutionLimit, javax.swing.JLayeredPane.DEFAULT_LAYER);

    descriptionRaw.setText(resourceMap.getString("descriptionRaw.text")); // NOI18N
    descriptionRaw.setName("descriptionRaw"); // NOI18N
    descriptionRaw.setBounds(140, 140, 380, 28);
    jLayeredPane2.add(descriptionRaw, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel13.setText(resourceMap.getString("jLabel13.text")); // NOI18N
    jLabel13.setName("jLabel13"); // NOI18N
    jLabel13.setBounds(140, 230, 190, 20);
    jLayeredPane2.add(jLabel13, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
    jLabel10.setName("jLabel10"); // NOI18N
    jLabel10.setBounds(140, 120, 170, 16);
    jLayeredPane2.add(jLabel10, javax.swing.JLayeredPane.DEFAULT_LAYER);

    pathToDataRaw.setText(resourceMap.getString("pathToDataRaw.text")); // NOI18N
    pathToDataRaw.setName("pathToDataRaw"); // NOI18N
    pathToDataRaw.setBounds(140, 88, 380, 20);
    jLayeredPane2.add(pathToDataRaw, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
    jLabel9.setName("jLabel9"); // NOI18N
    jLabel9.setBounds(140, 60, 150, 16);
    jLayeredPane2.add(jLabel9, javax.swing.JLayeredPane.DEFAULT_LAYER);

    browseRaw.setText(resourceMap.getString("browseRaw.text")); // NOI18N
    browseRaw.setName("browseRaw"); // NOI18N
    browseRaw.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            browseRawActionPerformed(evt);
        }
    });
    browseRaw.setBounds(530, 80, 100, 30);
    jLayeredPane2.add(browseRaw, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
    jLabel3.setName("jLabel3"); // NOI18N
    jLabel3.setBounds(130, 10, 150, 16);
    jLayeredPane2.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel16.setText(resourceMap.getString("jLabel16.text")); // NOI18N
    jLabel16.setName("jLabel16"); // NOI18N
    jLabel16.setBounds(140, 390, 180, 16);
    jLayeredPane2.add(jLabel16, javax.swing.JLayeredPane.DEFAULT_LAYER);

    mosaicSpread.setText(resourceMap.getString("mosaicSpread.text")); // NOI18N
    mosaicSpread.setName("mosaicSpread"); // NOI18N
    mosaicSpread.setBounds(140, 410, 180, 28);
    jLayeredPane2.add(mosaicSpread, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel17.setText(resourceMap.getString("jLabel17.text")); // NOI18N
    jLabel17.setName("jLabel17"); // NOI18N
    jLabel17.setBounds(140, 450, 150, 16);
    jLayeredPane2.add(jLabel17, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jScrollPane2.setName("jScrollPane2"); // NOI18N

    jList1.setModel(new javax.swing.AbstractListModel() {
        String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
        public int getSize() { return strings.length; }
        public Object getElementAt(int i) { return strings[i]; }
    });
    jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    jList1.setName("jList1"); // NOI18N
    jScrollPane2.setViewportView(jList1);

    jLayeredPane1.setName("jLayeredPane1"); // NOI18N

    removePDBs.setText(resourceMap.getString("removePDBs.text")); // NOI18N
    removePDBs.setName("removePDBs"); // NOI18N
    removePDBs.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            removePDBsActionPerformed(evt);
        }
    });
    removePDBs.setBounds(110, 490, 93, 29);
    jLayeredPane1.add(removePDBs, javax.swing.JLayeredPane.DEFAULT_LAYER);

    abstractExperimentScroll.setName("abstractExperimentScroll"); // NOI18N

    abstractExperiment.setColumns(20);
    abstractExperiment.setLineWrap(true);
    abstractExperiment.setRows(5);
    abstractExperiment.setWrapStyleWord(true);
    abstractExperiment.setName("abstractExperiment"); // NOI18N
    abstractExperimentScroll.setViewportView(abstractExperiment);

    abstractExperimentScroll.setBounds(13, 230, 540, 160);
    jLayeredPane1.add(abstractExperimentScroll, javax.swing.JLayeredPane.DEFAULT_LAYER);

    removeCitationURLs.setText(resourceMap.getString("removeCitationURLs.text")); // NOI18N
    removeCitationURLs.setName("removeCitationURLs"); // NOI18N
    removeCitationURLs.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            removeCitationURLsActionPerformed(evt);
        }
    });
    removeCitationURLs.setBounds(447, 490, 100, 29);
    jLayeredPane1.add(removeCitationURLs, javax.swing.JLayeredPane.DEFAULT_LAYER);

    authors.setText(resourceMap.getString("authors.text")); // NOI18N
    authors.setName("authors"); // NOI18N
    authors.setBounds(13, 172, 380, 28);
    jLayeredPane1.add(authors, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
    jLabel7.setName("jLabel7"); // NOI18N
    jLabel7.setBounds(15, 400, 140, 16);
    jLayeredPane1.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel20.setText(resourceMap.getString("jLabel20.text")); // NOI18N
    jLabel20.setName("jLabel20"); // NOI18N
    jLabel20.setBounds(13, 40, 170, 16);
    jLayeredPane1.add(jLabel20, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
    jLabel1.setName("jLabel1"); // NOI18N
    jLabel1.setBounds(3, 10, 160, 16);
    jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

    pathForDataExperiment.setText(resourceMap.getString("pathForDataExperiment.text")); // NOI18N
    pathForDataExperiment.setName("pathForDataExperiment"); // NOI18N
    pathForDataExperiment.setBounds(13, 110, 380, 30);
    jLayeredPane1.add(pathForDataExperiment, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
    jLabel5.setName("jLabel5"); // NOI18N
    jLabel5.setBounds(13, 150, 230, 20);
    jLayeredPane1.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
    jLabel4.setName("jLabel4"); // NOI18N
    jLabel4.setBounds(13, 90, 220, 20);
    jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
    jLabel6.setName("jLabel6"); // NOI18N
    jLabel6.setBounds(13, 210, 130, 20);
    jLayeredPane1.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);

    PDBsScroll.setName("PDBsScroll"); // NOI18N

    PDBs.setName("PDBs"); // NOI18N
    PDBsScroll.setViewportView(PDBs);

    PDBsScroll.setBounds(13, 420, 170, 60);
    jLayeredPane1.add(PDBsScroll, javax.swing.JLayeredPane.DEFAULT_LAYER);

    addPDBs.setText(resourceMap.getString("addPDBs.text")); // NOI18N
    addPDBs.setName("addPDBs"); // NOI18N
    addPDBs.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            addPDBsActionPerformed(evt);
        }
    });
    addPDBs.setBounds(30, 490, 70, 29);
    jLayeredPane1.add(addPDBs, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
    jLabel8.setName("jLabel8"); // NOI18N
    jLabel8.setBounds(220, 400, 160, 16);
    jLayeredPane1.add(jLabel8, javax.swing.JLayeredPane.DEFAULT_LAYER);

    citationURLsScroll.setName("citationURLsScroll"); // NOI18N

    citationURLs.setName("citationURLs"); // NOI18N
    citationURLsScroll.setViewportView(citationURLs);

    citationURLsScroll.setBounds(220, 420, 330, 60);
    jLayeredPane1.add(citationURLsScroll, javax.swing.JLayeredPane.DEFAULT_LAYER);

    addCitationURLs.setText(resourceMap.getString("addCitationURLs.text")); // NOI18N
    addCitationURLs.setName("addCitationURLs"); // NOI18N
    addCitationURLs.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            addCitationURLsActionPerformed(evt);
        }
    });
    addCitationURLs.setBounds(347, 490, 100, 29);
    jLayeredPane1.add(addCitationURLs, javax.swing.JLayeredPane.DEFAULT_LAYER);

    browseExperiment.setText(resourceMap.getString("browseExperiment.text")); // NOI18N
    browseExperiment.setName("browseExperiment"); // NOI18N
    browseExperiment.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            browseExperimentActionPerformed(evt);
        }
    });
    browseExperiment.setBounds(420, 109, 100, 30);
    jLayeredPane1.add(browseExperiment, javax.swing.JLayeredPane.DEFAULT_LAYER);

    institutionName.setText(resourceMap.getString("institutionName.text")); // NOI18N
    institutionName.setName("institutionName"); // NOI18N
    institutionName.setBounds(13, 550, 240, 28);
    jLayeredPane1.add(institutionName, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
    jLabel11.setName("jLabel11"); // NOI18N
    jLabel11.setBounds(13, 530, 170, 16);
    jLayeredPane1.add(jLabel11, javax.swing.JLayeredPane.DEFAULT_LAYER);

    titleExperiment.setText(resourceMap.getString("titleExperiment.text")); // NOI18N
    titleExperiment.setName("titleExperiment"); // NOI18N
    titleExperiment.setBounds(13, 60, 380, 28);
    jLayeredPane1.add(titleExperiment, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLayeredPane4.setName("jLayeredPane4"); // NOI18N

    jScrollPane1.setName("jScrollPane1"); // NOI18N

    console.setColumns(20);
    console.setRows(5);
    console.setWrapStyleWord(true);
    console.setName("console"); // NOI18N
    jScrollPane1.setViewportView(console);

    jScrollPane1.setBounds(10, 40, 550, 520);
    jLayeredPane4.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel21.setText(resourceMap.getString("jLabel21.text")); // NOI18N
    jLabel21.setName("jLabel21"); // NOI18N
    jLabel21.setBounds(16, 10, 210, 16);
    jLayeredPane4.add(jLabel21, javax.swing.JLayeredPane.DEFAULT_LAYER);

    javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(tardisdatadepositor.TARDISDataDepositorApp.class).getContext().getActionMap(TARDISDataDepositorView.class, this);
    jButton2.setAction(actionMap.get("newAncillaryDataset")); // NOI18N
    jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
    jButton2.setName("jButton2"); // NOI18N

    jButton3.setAction(actionMap.get("newRawDataset")); // NOI18N
    jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
    jButton3.setName("jButton3"); // NOI18N

    jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
    jButton1.setName("jButton1"); // NOI18N
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    packageButton.setText(resourceMap.getString("packageButton.text")); // NOI18N
    packageButton.setName("packageButton"); // NOI18N
    packageButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            packageButtonActionPerformed(evt);
        }
    });

    showConsole.setText(resourceMap.getString("showConsole.text")); // NOI18N
    showConsole.setName("showConsole"); // NOI18N
    showConsole.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            showConsoleActionPerformed(evt);
        }
    });

    jLabel22.setText(resourceMap.getString("jLabel22.text"));
    jLabel22.setName("jLabel22");

    org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
    mainPanel.setLayout(mainPanelLayout);
    mainPanelLayout.setHorizontalGroup(
        mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(mainPanelLayout.createSequentialGroup()
            .add(42, 42, 42)
            .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                .add(mainPanelLayout.createSequentialGroup()
                    .add(jScrollPane2)
                    .add(14, 14, 14))
                .add(mainPanelLayout.createSequentialGroup()
                    .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, packageButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, showConsole, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                        .add(jLabel22, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(18, 18, 18)))
            .add(37, 37, 37)
            .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 588, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(68, Short.MAX_VALUE))
        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanelLayout.createSequentialGroup()
                .add(157, 157, 157)
                .add(jLayeredPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 658, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(120, Short.MAX_VALUE)))
        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanelLayout.createSequentialGroup()
                .add(241, 241, 241)
                .add(jLayeredPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 665, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE)))
        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createSequentialGroup()
                .add(271, 271, 271)
                .add(jLayeredPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
                .add(56, 56, 56)))
    );
    mainPanelLayout.setVerticalGroup(
        mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createSequentialGroup()
            .addContainerGap()
            .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 626, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(mainPanelLayout.createSequentialGroup()
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jButton3)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                    .add(jButton2)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                    .add(jButton1)
                    .add(29, 29, 29)
                    .add(packageButton)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(showConsole)
                    .add(18, 18, 18)
                    .add(jLabel22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
            .add(837, 837, 837))
        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(jLayeredPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 580, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(883, Short.MAX_VALUE)))
        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanelLayout.createSequentialGroup()
                .add(22, 22, 22)
                .add(jLayeredPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 353, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(1108, Short.MAX_VALUE)))
        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(jLayeredPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 562, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(901, Short.MAX_VALUE)))
    );

    menuBar.setName("menuBar"); // NOI18N

    fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
    fileMenu.setName("fileMenu"); // NOI18N

    exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
    exitMenuItem.setName("exitMenuItem"); // NOI18N
    fileMenu.add(exitMenuItem);

    menuBar.add(fileMenu);

    helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
    helpMenu.setName("helpMenu"); // NOI18N

    aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
    aboutMenuItem.setName("aboutMenuItem"); // NOI18N
    helpMenu.add(aboutMenuItem);

    menuBar.add(helpMenu);

    statusPanel.setName("statusPanel"); // NOI18N

    statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

    statusMessageLabel.setName("statusMessageLabel"); // NOI18N

    statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
    statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

    org.jdesktop.layout.GroupLayout statusPanelLayout = new org.jdesktop.layout.GroupLayout(statusPanel);
    statusPanel.setLayout(statusPanelLayout);
    statusPanelLayout.setHorizontalGroup(
        statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(statusPanelLayout.createSequentialGroup()
            .add(statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 494, Short.MAX_VALUE)
                .add(statusPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .add(statusMessageLabel)))
            .add(421, 421, 421)
            .add(statusAnimationLabel)
            .addContainerGap())
    );
    statusPanelLayout.setVerticalGroup(
        statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(statusPanelLayout.createSequentialGroup()
            .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 31, Short.MAX_VALUE)
            .add(statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(statusMessageLabel)
                .add(statusAnimationLabel))
            .add(3, 3, 3))
    );

    jInternalFrame1.setName("jInternalFrame1"); // NOI18N
    jInternalFrame1.setVisible(true);

    org.jdesktop.layout.GroupLayout jInternalFrame1Layout = new org.jdesktop.layout.GroupLayout(jInternalFrame1.getContentPane());
    jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
    jInternalFrame1Layout.setHorizontalGroup(
        jInternalFrame1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(0, 0, Short.MAX_VALUE)
    );
    jInternalFrame1Layout.setVerticalGroup(
        jInternalFrame1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(0, 0, Short.MAX_VALUE)
    );

    jSeparator1.setName("jSeparator1"); // NOI18N

    popupMenu1.setLabel(resourceMap.getString("popupMenu1.label")); // NOI18N

    setComponent(mainPanel);
    setMenuBar(menuBar);
    setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void addPDBsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPDBsActionPerformed
//        try {
//            new DiffdumpOut("/Users/steve/Desktop/Datasets/1SNG/tfusca/tfusca_1_016.img").displayVars();
//        } catch (Exception ex) {
//            Logger.getLogger(TARDISDataDepositorView.class.getName()).log(Level.SEVERE, null, ex);
//        }
            InputDialog myDialog = new InputDialog(getFrame(), true, "Please enter a PDB ID:");
            //System.err.println("After opening dialog.");
            if(myDialog.getAnswer() != null) {
                String answer = myDialog.getAnswer();

                if(answer.length() > 0)
                {
                    pdbsListModel.addElement(answer);
                }
            }

    }//GEN-LAST:event_addPDBsActionPerformed

    private void browseExperimentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseExperimentActionPerformed
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnVal = fc.showOpenDialog(mainPanel);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                pathForDataExperiment.setText(file.getAbsolutePath());
                //This is where a real application would open the file.
                //log.append("Opening: " + file.getName() + "." + newline);
            } else {
               // log.append("Open command cancelled by user." + newline);
            }
    }//GEN-LAST:event_browseExperimentActionPerformed

    private void browseRawActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseRawActionPerformed
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnVal = fc.showOpenDialog(mainPanel);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                pathToDataRaw.setText(file.getAbsolutePath());
                //This is where a real application would open the file.
                //log.append("Opening: " + file.getName() + "." + newline);
            } else {
               // log.append("Open command cancelled by user." + newline);
            }
    }//GEN-LAST:event_browseRawActionPerformed

    private void browseAncillaryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseAncillaryActionPerformed
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnVal = fc.showOpenDialog(mainPanel);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                pathToDataAncillary.setText(file.getAbsolutePath());
                //This is where a real application would open the file.
                //log.append("Opening: " + file.getName() + "." + newline);
            } else {
               // log.append("Open command cancelled by user." + newline);
            }
    }//GEN-LAST:event_browseAncillaryActionPerformed

    private void addCitationURLsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCitationURLsActionPerformed
            InputDialog myDialog = new InputDialog(getFrame(), true, "Please enter a Citation URL:");
            //System.err.println("After opening dialog.");
            if(myDialog.getAnswer() != null) {
                String answer = myDialog.getAnswer();

                if(answer.length() > 0)
                {
                    citationsListModel.addElement(answer);
                }
            }
    }//GEN-LAST:event_addCitationURLsActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //stateMap.remove(jList1.getSelectedIndex());
        if(!(jList1.getSelectedValue() instanceof ExperimentInfo))
        {
            if(jList1.getSelectedValues().length > 0)
            {
                listModel.remove(jList1.getSelectedIndex());
            }
        }

        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void execute()
    {
        ExperimentInfo experimentInfo = new ExperimentInfo(new ArrayList<String>(), "", new ArrayList<String>(), "", new ArrayList<String>(), new File(""), "");
        ArrayList datasetInfoList = new ArrayList<DatasetInfo>();

        Object currentlySelected = jList1.getModel().getElementAt(jList1.getSelectedIndex());
        saveInterfaceState(currentlySelected, jList1.getSelectedIndex());

        Object[] listModelArray = new Object[200];
        listModel.copyInto(listModelArray);

        for (int i = 0; i < listModelArray.length; i++)
        {

            Object item = listModelArray[i];

            if(item instanceof ExperimentInfo)
            {
                //saveInterfaceState(listModelArray[i], i);
                experimentInfo = (ExperimentInfo) item;

            }
            else if(item instanceof DatasetInfo)
            {
                //saveInterfaceState(listModelArray[i], i);
                DatasetInfo datasetInfo = (DatasetInfo) item;
                //System.out.println("Adding item " + i + " named " + datasetInfo.getDescription() + " to execution list");

                datasetInfoList.add(datasetInfo);

            }

        }

        Packager TARDISPackage = new Packager(datasetInfoList, experimentInfo);

        if (validateGo())
        {
            enableConsoleView();

            TARDISPackage.writeMETS("J-1", TARDISPackage.getExperimentInfo().getTitle(), "study",
					"Scientific Dataset Profile 1.0");
            //System.out.println("Writing package");
        }
    }


    private void packageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_packageButtonActionPerformed

        Thread worker = new Thread(new HardWorker());
        worker.setDaemon(true);
        worker.start();

}//GEN-LAST:event_packageButtonActionPerformed

    private void showConsoleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showConsoleActionPerformed
        if(jList1.getSelectedIndices().length > 0)
        {
            Object currentlySelected = jList1.getModel().getElementAt(jList1.getSelectedIndex());
            saveInterfaceState(currentlySelected, jList1.getSelectedIndex());
        }

        jList1.clearSelection();

        enableConsoleView();
    }//GEN-LAST:event_showConsoleActionPerformed

    private void removePDBsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removePDBsActionPerformed
        if(PDBs.getSelectedIndices().length > 0)
        {
            pdbsListModel.remove(PDBs.getSelectedIndex());
        }
    }//GEN-LAST:event_removePDBsActionPerformed

    private void removeCitationURLsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeCitationURLsActionPerformed
        if(citationURLs.getSelectedIndices().length > 0)
        {
            citationsListModel.remove(citationURLs.getSelectedIndex());
        }
    }//GEN-LAST:event_removeCitationURLsActionPerformed

    private boolean validateGo()
    {
       Pattern p = Pattern.compile("(^[0-9]*[1-9]+[0-9]*\\.[0-9]*$)|(^[0-9]*\\.[0-9]*[1-9]+[0-9]*$)|(^[0-9]*[1-9]+[0-9]*$)");
       // check for positive decimal value
        try {
            ExperimentInfo experimentInfo = new ExperimentInfo(new ArrayList<String>(), "", new ArrayList<String>(), "", new ArrayList<String>(), new File(""), "");

            if(jList1.getModel().getSize() < 2)
            {
                throw new Exception("Packages require at least one dataset");
            }

            for (int i = 0; i < jList1.getModel().getSize(); i++)
            {

                Object item = jList1.getModel();

                if(item instanceof ExperimentInfo)
                {
                    experimentInfo = (ExperimentInfo) item;

                    if(experimentInfo.getTitle().equals(""))
                    {
                        throw new Exception("Missing Experiment Title");
                    }
                    else if(experimentInfo.getAuthors().size() == 0)
                    {
                        throw new Exception("Missing Experiment Authors");
                    }
                    else if(experimentInfo.getabstractTxt().equals(""))
                    {
                        throw new Exception("Missing Experiment Abstract");
                    }
                    else if(experimentInfo.getInstitutionName().equals(""))
                    {
                        throw new Exception("Missing Experiment Institution Name");
                    }

                }
                if(item instanceof DatasetInfo)
                {
                    DatasetInfo datasetInfo = (DatasetInfo) item;

                    System.out.println("Checking dataset " + datasetInfo.getDescription());

                    if(datasetInfo.isRawImages())
                    {
                        if(datasetInfo.getDescription().equals(""))
                        {
                            throw new Exception("Missing Dataset Description");
                        }
                        else if(datasetInfo.getCrystalName().equals(""))
                        {
                            throw new Exception("Missing Crystal Name in Dataset: " + datasetInfo.getDescription());
                        }
                        else if(datasetInfo.getDiffractometerType().equals(""))
                        {
                            throw new Exception("Missing Diffractometer Type in Dataset: " + datasetInfo.getDescription());
                        }
                        else if(datasetInfo.getXraySource().equals(""))
                        {
                            throw new Exception("Missing Xray Source in Dataset: " + datasetInfo.getDescription());
                        }
                        else if(!new File(datasetInfo.getDir()).exists())
                        {
                            throw new Exception("Invalid Path to Data in Dataset: " + datasetInfo.getDescription());
                        }
                        else if(!datasetInfo.getMosaicSpread().equals(""))
                        {
                            Float.parseFloat(mosaicSpread.getText());
                        }
                        else if(!datasetInfo.getChiAngle().equals(""))
                        {
                            Float.parseFloat(crystalName.getText());
                        }

                    }
                    else
                    {
                        if(datasetInfo.getDescription().equals(""))
                        {
                            throw new Exception("Missing Dataset Description");
                        }
                        else if(!new File(datasetInfo.getDir()).exists())
                        {
                            throw new Exception("Invalid Path to Data in Dataset: " + datasetInfo.getDescription());
                        }
                    }
                }

                
            }
            
        }
        catch (NumberFormatException e)
        {
            JOptionPane.showMessageDialog(mainPanel, "Error: Some non-numerical values were found in numerical fields",
				                       "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(mainPanel, e.getMessage(),
				                       "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        return true;

    }


    @Action
    public void newRawDataset() {
        listModel.addElement(new DatasetInfo("", "", "", "", 0f, "", "", ""));
    }

    @Action
    public void newAncillaryDataset() {
        listModel.addElement(new DatasetInfo("", ""));
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected ImageIcon createImageIcon(String path,
                                               String description) {
        java.net.URL imgURL = getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList PDBs;
    private javax.swing.JScrollPane PDBsScroll;
    private javax.swing.JTextArea abstractExperiment;
    private javax.swing.JScrollPane abstractExperimentScroll;
    private javax.swing.JButton addCitationURLs;
    private javax.swing.JButton addPDBs;
    private javax.swing.JTextField authors;
    private javax.swing.JButton browseAncillary;
    private javax.swing.JButton browseExperiment;
    private javax.swing.JButton browseRaw;
    private javax.swing.JTextField chiAngle;
    private javax.swing.JList citationURLs;
    private javax.swing.JScrollPane citationURLsScroll;
    private javax.swing.JTextArea console;
    private javax.swing.JTextField crystalName;
    private javax.swing.JTextField descriptionAncillary;
    private javax.swing.JTextField descriptionRaw;
    private javax.swing.JTextField diffractometerType;
    private javax.swing.JTextField institutionName;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JLayeredPane jLayeredPane3;
    private javax.swing.JLayeredPane jLayeredPane4;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JTextField mosaicSpread;
    private javax.swing.JButton packageButton;
    private javax.swing.JTextField pathForDataExperiment;
    private javax.swing.JTextField pathToDataAncillary;
    private javax.swing.JTextField pathToDataRaw;
    private java.awt.PopupMenu popupMenu1;
    private javax.swing.JButton removeCitationURLs;
    private javax.swing.JButton removePDBs;
    private javax.swing.JTextField resolutionLimit;
    private javax.swing.JButton showConsole;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JTextField titleExperiment;
    private javax.swing.JTextField xraySource;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;


      /**
      * Perform the fetch, and update the GUI elements using the event dispatch
      * thread.
      *
      * Should be run as a daemon thread, such that it never prevents the application
      * from exiting.
      */
      private final class HardWorker implements Runnable {
        public void run() {
          //simulateLongDelay();
          try {
            // do stuff here
              execute();
            //EventQueue.invokeLater( new GuiUpdater(quotes) );
          }
          catch(Exception ex) {
            //EventQueue.invokeLater( new GuiUpdater(ex) );
          }
        }
      }
}